package com.demo.androidgame.samgukji_hit;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class UnitScreen extends GLScreen{

	Camera2D guiCam;
	SpriteBatcher batcher;
	Rectangle ok_bt;
	Rectangle cancel_bt;
	int selectherono;
	Vector2 touchPoint;
	
	public UnitScreen(Game game) {
		super(game);
		
		guiCam = new Camera2D(glGraphics, 800, 480);
		batcher = new SpriteBatcher(glGraphics, 100);
		ok_bt = new Rectangle(680, 20, 100, 40);
		cancel_bt = new Rectangle(20, 20, 100, 40);
		touchPoint = new Vector2();
	}

	@Override
	public void update(float deltaTime) {
		selectherono = SelectScreen.herono;
		
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                        
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);
                if (OverlapTester.pointInRectangle(ok_bt, touchPoint)) {
                	game.setScreen(new ActScreen(game));
                	return;
                }
                if (OverlapTester.pointInRectangle(cancel_bt, touchPoint)) {
                	game.setScreen(new SelectScreen(game));
                	return;
                }
            }
        }
		
	}


	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
		batcher.beginBatch(Assets.nation_bar);
		if (selectherono == Assets.HeroNo1 || selectherono == Assets.HeroNo2) {
			batcher.drawSprite(400, 240, 780, 460, Assets.nation_red);
			
		} else if (selectherono == Assets.HeroNo3 || selectherono == Assets.HeroNo4) {
			batcher.drawSprite(400, 240, 780, 460, Assets.nation_green);
			
		} else if (selectherono == Assets.HeroNo5 || selectherono == Assets.HeroNo6) {
			batcher.drawSprite(400, 240, 780, 460, Assets.nation_blue);
			
		}
		batcher.endBatch();
		
		
		if (selectherono == Assets.HeroNo1) {
			
		} else if (selectherono == Assets.HeroNo2) {
			batcher.beginBatch(Assets.mujang2_2);
			batcher.drawSprite(250, 250, 200, 400, Assets.mujang2_2Region);
			batcher.endBatch();
		} else if (selectherono == Assets.HeroNo3) {
			batcher.beginBatch(Assets.mujang3_2);
			batcher.drawSprite(250, 250, 200, 400, Assets.mujang3_2Region);
			batcher.endBatch();
		} else if (selectherono == Assets.HeroNo4) {
			batcher.beginBatch(Assets.mujang4_2);
			batcher.drawSprite(250, 250, 200, 400, Assets.mujang4_2Region);
			batcher.endBatch();
		} else if (selectherono == Assets.HeroNo5) {
			
		} else if (selectherono == Assets.HeroNo6) {
			
		}
		
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		batcher.beginBatch(Assets.ok_cancel_bt);
		batcher.drawSprite(730, 40, 100, 40, Assets.ok_btRegion);
		batcher.drawSprite(70, 40, 100, 40, Assets.cancelbtRegion);
		batcher.endBatch();
		
		gl.glDisable(GL10.GL_BLEND);
		gl.glDisable(GL10.GL_TEXTURE_2D);
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}
